﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using HIWSystem.Common;
using HIWSystem.Helpers;
using HIWSystem.Models;

namespace HIWSystem.ViewModel
{
    public class PlanOutputInfoQueryViewModel :ViewModelBase
    {
        #region 属性
        DataTable dtOutput = new DataTable();

        private bool isFirstFilterWO = true;
        private bool isFirstFilterPN = true;
        private bool isFirstFilterStation = true;
        private bool isFirstFilterWeekDay = true;
        private bool isFirstFilterWeek = true;
        private bool isFirstFilterYear = true;
        ObservableCollection<WOEntity> backupWOs = null;
        ObservableCollection<PartNumberEntity> backupPNs = null;
        ObservableCollection<StationsEntity> backupStations = null;
        ObservableCollection<ProductionPlanAndActualEntity> backupWeekDays = null;
        ObservableCollection<ProductionPlanEntity> backupWeeks = null;
        ObservableCollection<ProductionPlanEntity> backupYears = null;

        private ObservableCollection<WOEntity> wos;
        /// <summary>
        /// 工单集合
        /// </summary>
        public ObservableCollection<WOEntity> WOs
        {
            get { return wos; }
            set { wos = value; this.RaisePropertyChanged(() => WOs); }
        }
        private WOEntity selectedWO;
        /// <summary>
        /// 选择的工单实体
        /// </summary>
        public WOEntity SelectedWO
        {
            get { return selectedWO; }
            set { selectedWO = value; this.RaisePropertyChanged(() => SelectedWO); }
        }
        private string wo;
        /// <summary>
        /// 工单
        /// </summary>
        public string WO
        {
            get { return wo; }
            set { wo = value; this.RaisePropertyChanged(() => WO); }
        }
        private bool isOpenComboWODropDown;
        /// <summary>
        /// 是否打开下拉框面板
        /// </summary>
        public bool IsOpenComboWODropDown
        {
            get { return isOpenComboWODropDown; }
            set { isOpenComboWODropDown = value; this.RaisePropertyChanged(() => IsOpenComboWODropDown); }
        }
        private string searchWOText;
        /// <summary>
        /// 查询文本
        /// </summary>
        public string SearchWOText
        {
            get { return searchWOText; }
            set { searchWOText = value; this.RaisePropertyChanged(() => SearchWOText); }
        }
        private ObservableCollection<PartNumberEntity> pns;
        /// <summary>
        /// 机种实体集合
        /// </summary>
        public ObservableCollection<PartNumberEntity> PNs
        {
            get { return pns; }
            set { pns = value; this.RaisePropertyChanged(() => PNs); }
        }
        private PartNumberEntity selectedPN;
        /// <summary>
        /// 选择的机种实体
        /// </summary>
        public PartNumberEntity SelectedPN
        {
            get { return selectedPN; }
            set { selectedPN = value; this.RaisePropertyChanged(() => SelectedPN); }
        }
        private string pn;
        /// <summary>
        /// 机种
        /// </summary>
        public string PN
        {
            get { return pn; }
            set { pn = value; this.RaisePropertyChanged(() => PN); }
        }
        private bool isOpenComboPNDropDown;
        /// <summary>
        /// 是否打开机种下拉框选项卡
        /// </summary>
        public bool IsOpenComboPNDropDown
        {
            get { return isOpenComboPNDropDown; }
            set { isOpenComboPNDropDown = value; this.RaisePropertyChanged(() => IsOpenComboPNDropDown); }
        }
        private string searchPNText;
        /// <summary>
        /// 机种下拉框中输入的文本内容
        /// </summary>
        public string SearchPNText
        {
            get { return searchPNText; }
            set { searchPNText = value; this.RaisePropertyChanged(() => SearchPNText); }
        }
        private ObservableCollection<StationsEntity> stations;
        /// <summary>
        /// 工序集合
        /// </summary>
        public ObservableCollection<StationsEntity> Stations
        {
            get { return stations; }
            set { stations = value; this.RaisePropertyChanged(() => Stations); }
        }
        private StationsEntity selectedStation;
        /// <summary>
        /// 选择的工序实体
        /// </summary>
        public StationsEntity SelectedStation
        {
            get { return selectedStation; }
            set { selectedStation = value; this.RaisePropertyChanged(() => SelectedStation); }
        }

        private string station;
        /// <summary>
        /// 站位
        /// </summary>
        public string Station
        {
            get { return station; }
            set { station = value; this.RaisePropertyChanged(() => Station); }
        }
        private bool isOpenComboStationDropDown;
        /// <summary>
        /// 是否打开工序下拉列表框
        /// </summary>
        public bool IsOpenComboStationDropDown
        {
            get { return isOpenComboStationDropDown; }
            set { isOpenComboStationDropDown = value; this.RaisePropertyChanged(() => IsOpenComboStationDropDown); }
        }
        private string searchStationText;
        /// <summary>
        /// 选择的工序文本
        /// </summary>
        public string SearchStationText
        {
            get { return searchStationText; }
            set { searchStationText = value; this.RaisePropertyChanged(() => SearchStationText); }
        }
        private ObservableCollection<ProductionPlanAndActualEntity> weekDays;
        /// <summary>
        /// 星期集合
        /// </summary>
        public ObservableCollection<ProductionPlanAndActualEntity> WeekDays
        {
            get { return weekDays; }
            set { weekDays = value; this.RaisePropertyChanged(() => WeekDays); }
        }
        private ProductionPlanAndActualEntity selectedWeekDay;
        /// <summary>
        /// 星期实体
        /// </summary>
        public ProductionPlanAndActualEntity SelectedWeekDay
        {
            get { return selectedWeekDay; }
            set { selectedWeekDay = value; this.RaisePropertyChanged(() => SelectedWeekDay); }
        }
        private string weekDayValue="All";
        /// <summary>
        /// 星期的值(1,2,3,4,5,6,7)
        /// </summary>
        public string WeekDayValue
        {
            get { return weekDayValue; }
            set { weekDayValue = value; this.RaisePropertyChanged(() => WeekDayValue); }
        }
        private bool isOpenComboWeekDayDropDown;
        /// <summary>
        /// 是否开启星期下拉列表框
        /// </summary>
        public bool IsOpenComboWeekDayDropDown
        {
            get { return isOpenComboWeekDayDropDown; }
            set { isOpenComboWeekDayDropDown = value; this.RaisePropertyChanged(() => IsOpenComboWeekDayDropDown); }
        }
        private string searchWeekDayText;
        /// <summary>
        /// 星期的搜素内容
        /// </summary>
        public string SearchWeekDayText
        {
            get { return searchWeekDayText; }
            set { searchWeekDayText = value; this.RaisePropertyChanged(() => SearchWeekDayText); }
        }
        private ObservableCollection<ProductionPlanEntity> weeks;
        /// <summary>
        /// 周别集合
        /// </summary>
        public ObservableCollection<ProductionPlanEntity> Weeks
        {
            get { return weeks; }
            set { weeks = value; this.RaisePropertyChanged(() => Weeks); }
        }
        private ProductionPlanEntity selectedWeek;
        /// <summary>
        /// 选择的周别实体
        /// </summary>
        public ProductionPlanEntity SelectedWeek
        {
            get { return selectedWeek; }
            set { selectedWeek = value; this.RaisePropertyChanged(() => SelectedWeek); }
        }
        private string week;
        /// <summary>
        /// 选择的周别
        /// </summary>
        public string Week
        {
            get { return week; }
            set { week = value; this.RaisePropertyChanged(() => Week); }
        }
        private bool isOpenComboWeekDropDown;
        /// <summary>
        /// 是否打开周别下拉列表框
        /// </summary>
        public bool IsOpenComboWeekDropDown
        {
            get { return isOpenComboWeekDropDown; }
            set { isOpenComboWeekDropDown = value; this.RaisePropertyChanged(() => IsOpenComboWeekDropDown); }
        }
        private string searchWeekText;
        /// <summary>
        /// 周别查询文本
        /// </summary>
        public string SearchWeekText
        {
            get { return searchWeekText; }
            set { searchWeekText = value; this.RaisePropertyChanged(() => SearchWeekText); }
        }
        private ObservableCollection<ProductionPlanEntity> shifts;
        /// <summary>
        /// 班别
        /// </summary>
        public ObservableCollection<ProductionPlanEntity> Shifts
        {
            get { return shifts; }
            set { shifts = value; this.RaisePropertyChanged(() => Shifts); }
        }
        private ProductionPlanEntity selectedShift;
        /// <summary>
        /// 班别实体
        /// </summary>
        public ProductionPlanEntity SelectedShift
        {
            get { return selectedShift; }
            set { selectedShift = value; this.RaisePropertyChanged(() => SelectedShift); }
        }
        private string shift;
        /// <summary>
        /// 班别
        /// </summary>
        public string Shift
        {
            get { return shift; }
            set { shift = value; this.RaisePropertyChanged(() => Shift); }
        }
        private ObservableCollection<ProductionPlanEntity> years;
        /// <summary>
        /// 年份集合
        /// </summary>
        public ObservableCollection<ProductionPlanEntity> Years
        {
            get { return years; }
            set { years = value; this.RaisePropertyChanged(() => Years); }
        }
        private ProductionPlanEntity selectedYear;
        /// <summary>
        /// 选择的周别实体
        /// </summary>
        public ProductionPlanEntity SelectedYear
        {
            get { return selectedYear; }
            set { selectedYear = value; this.RaisePropertyChanged(() => SelectedYear); }
        }
        private string year;
        /// <summary>
        /// 选择的周别
        /// </summary>
        public string Year
        {
            get { return year; }
            set { year = value; this.RaisePropertyChanged(() => Year); }
        }
        private bool isOpenComboYearDropDown;
        /// <summary>
        /// 是否打开周别下拉列表框
        /// </summary>
        public bool IsOpenComboYearDropDown
        {
            get { return isOpenComboYearDropDown; }
            set { isOpenComboYearDropDown = value; this.RaisePropertyChanged(() => IsOpenComboYearDropDown); }
        }
        private string searchYearText;
        /// <summary>
        /// 周别查询文本
        /// </summary>
        public string SearchYearText
        {
            get { return searchYearText; }
            set { searchYearText = value; this.RaisePropertyChanged(() => SearchYearText); }
        }
        private bool isEnabledExportButton = true;
        /// <summary>
        /// 是否启用导出按钮
        /// </summary>
        public bool IsEnabledExportButton
        {
            get { return isEnabledExportButton; }
            set { isEnabledExportButton = value; this.RaisePropertyChanged(() => IsEnabledExportButton); }
        }
        private bool isEnabledQueryButton = true;
        /// <summary>
        /// 是否启用查询按钮
        /// </summary>
        public bool IsEnabledQueryButton
        {
            get { return isEnabledQueryButton; }
            set { isEnabledQueryButton = value; this.RaisePropertyChanged(() => IsEnabledQueryButton); }
        }
        private Visibility isVisibleLoading = Visibility.Collapsed;
        /// <summary>
        /// 是否显示加载图标
        /// </summary>
        public Visibility IsVisibleLoading
        {
            get { return isVisibleLoading; }
            set { isVisibleLoading = value; this.RaisePropertyChanged(() => IsVisibleLoading); }
        }
        private ObservableCollection<ProductionPlanAndActualEntity> outputs;
        /// <summary>
        /// 产量集合
        /// </summary>
        public ObservableCollection<ProductionPlanAndActualEntity> Outputs
        {
            get { return outputs; }
            set { outputs = value; this.RaisePropertyChanged(() => Outputs); }
        }

        #endregion

        #region 命令
        private RelayCommand loadPageCmd;

        public RelayCommand LoadPageCmd
        {
            get
            {
                loadPageCmd = loadPageCmd ?? new RelayCommand(LoadedPage);
                return loadPageCmd;
            }
            set { loadPageCmd = value; }
        }
        private RelayCommand filterWOCmd;

        public RelayCommand FilterWOCmd
        {
            get
            {
                filterWOCmd = filterWOCmd ?? new RelayCommand(FilterWO);
                return filterWOCmd;
            }
            set { filterWOCmd = value; }
        }
        private RelayCommand filterPNCmd;

        public RelayCommand FilterPNCmd
        {
            get
            {
                if (filterPNCmd == null)
                {
                    filterPNCmd = new RelayCommand(FilterPN);
                }
                return filterPNCmd;
            }
            set { filterPNCmd = value; }
        }
        private RelayCommand filterStationCmd;

        public RelayCommand FilterStationCmd
        {
            get
            {
                filterStationCmd = filterStationCmd ?? new RelayCommand(FilterStation);
                return filterStationCmd;
            }
            set { filterStationCmd = value; }
        }
        private RelayCommand filterWeekDayCmd;

        public RelayCommand FilterWeekDayCmd
        {
            get
            {
                filterWeekDayCmd = filterWeekDayCmd ?? new RelayCommand(FilterWeekDay);
                return filterWeekDayCmd;
            }
            set { filterWeekDayCmd = value; }
        }
        private RelayCommand filterWeekCmd;

        public RelayCommand FilterWeekCmd
        {
            get
            {
                filterWeekCmd = filterWeekCmd ?? new RelayCommand(FilterWeek);
                return filterWeekCmd;
            }
            set { filterWeekCmd = value; }
        }
        private RelayCommand filterYearCmd;

        public RelayCommand FilterYearCmd
        {
            get
            {
                filterYearCmd = filterYearCmd ?? new RelayCommand(FilterYear);
                return filterYearCmd;
            }
            set { filterYearCmd = value; }
        }
        private RelayCommand queryCmd;

        public RelayCommand QueryCmd
        {
            get {
                queryCmd = queryCmd ?? new RelayCommand(QueryAsync);
                return queryCmd; }
            set { queryCmd = value; }
        }
        private RelayCommand exportCmd;

        public RelayCommand ExportCmd
        {
            get {
                exportCmd = exportCmd ?? new RelayCommand(ExportAsync);
                return exportCmd; }
            set { exportCmd = value; }
        }

        #endregion

        #region 方法
        /// <summary>
        /// 查询数据
        /// </summary>
        private async void QueryAsync()
        {
            string retMsg = string.Empty;
            string[] arrMsg = new string[2];

            if (IsEnabledExportButton == false)
            {
                MessageBox.Show("正在导出报告...待导出完成以后再查询", "软件温馨提示!", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            //ObservableCollection<TrackingEntity> tmpTunningLogs = new ObservableCollection<TrackingEntity>();
            ObservableCollection<TunningYieldEntity> tmpTunningYield = new ObservableCollection<TunningYieldEntity>();

            await Task.Run(() =>
            {
                try
                {
                    Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                    {
                            //禁用查询按钮
                            this.IsEnabledQueryButton = false;
                            //显示加载图标
                            this.IsVisibleLoading = Visibility.Visible;
                        Outputs = new ObservableCollection<ProductionPlanAndActualEntity>();
                            //TunningLogs = new ObservableCollection<TrackingEntity>();
                        }));
                    ObservableCollection<ProductionPlanAndActualEntity> tmpOutputs =
                                                    new ObservableCollection<ProductionPlanAndActualEntity>();
                    ProductionPlanAndActualEntity entity = new ProductionPlanAndActualEntity
                    {
                        WO = this.WO == "全部" ? "" : (this.WO ?? ""),
                        PN = this.PN == "全部" ? "" : (this.PN ?? ""),
                        Station = this.Station == "全部" ? "" : (this.Station ?? ""),
                        WeekDay = WeekDayValue == "All" ? "" : (this.WeekDayValue ?? ""),
                        Week = this.Week ?? "",
                        Year = this.Year ?? DateTime.Now.Year.ToString(),
                        Shift = this.Shift == "全部" ?"": (this.Shift ?? "")
                    };
                    dtOutput = new DataTable();
                    retMsg = BusinessProcess.QueryProductionPlanAndActualOutput(entity, out dtOutput);
                    arrMsg = retMsg.Split('|');
                    if (arrMsg[0] == "OK")
                    {
                        for (int i = 0; i < dtOutput.Rows.Count; i++)
                        {
                            tmpOutputs.Add(new ProductionPlanAndActualEntity
                            {
                                Station = dtOutput.Rows[i]["Station"].ToString(),
                                WO = dtOutput.Rows[i]["WO"].ToString(),
                                WOCount = Convert.ToInt32(dtOutput.Rows[i]["Count"]==DBNull.Value?"0": dtOutput.Rows[i]["Count"].ToString()),
                                PN = dtOutput.Rows[i]["PN"].ToString(),
                                MondayPlan = dtOutput.Columns.Contains("MondayPlan") ? dtOutput.Rows[i]["MondayPlan"].ToString() : "",
                                MondayActual = dtOutput.Columns.Contains("MondayActual") ? dtOutput.Rows[i]["MondayActual"].ToString() : "",
                                TuesdayPlan = dtOutput.Columns.Contains("TuesdayPlan") ? dtOutput.Rows[i]["TuesdayPlan"].ToString() : "",
                                TuesdayActual = dtOutput.Columns.Contains("TuesdayActual") ? dtOutput.Rows[i]["TuesdayActual"].ToString() : "",
                                WednesdayPlan = dtOutput.Columns.Contains("WednesdayPlan") ? dtOutput.Rows[i]["WednesdayPlan"].ToString() : "",
                                WednesdayActual = dtOutput.Columns.Contains("WednesdayActual") ? dtOutput.Rows[i]["WednesdayActual"].ToString() : "",
                                ThursdayPlan = dtOutput.Columns.Contains("ThursdayPlan") ? dtOutput.Rows[i]["ThursdayPlan"].ToString() : "",
                                ThursdayActual = dtOutput.Columns.Contains("ThursdayActual") ? dtOutput.Rows[i]["ThursdayActual"].ToString() : "",
                                FirdayPlan = dtOutput.Columns.Contains("FirdayPlan") ? dtOutput.Rows[i]["FirdayPlan"].ToString() : "",
                                FirdayActual = dtOutput.Columns.Contains("FirdayActual") ? dtOutput.Rows[i]["FirdayActual"].ToString() : "",
                                SaturdayPlan = dtOutput.Columns.Contains("SaturdayPlan") ? dtOutput.Rows[i]["SaturdayPlan"].ToString() : "",
                                SaturdayActual = dtOutput.Columns.Contains("SaturdayActual") ? dtOutput.Rows[i]["SaturdayActual"].ToString() : "",
                                SundayPlan = dtOutput.Columns.Contains("SundayPlan") ? dtOutput.Rows[i]["SundayPlan"].ToString() : "",
                                SundayActual = dtOutput.Columns.Contains("SundayActual") ? dtOutput.Rows[i]["SundayActual"].ToString() : "",
                            }); ; ;
                        }
                        Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                        {
                                //this.TunningLogs = tmpTunningLogs;
                                this.Outputs = tmpOutputs;
                        }));
                    }
                    else
                    {
                        MessageBox.Show(arrMsg[1], "软件温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                    }

                }
                catch (Exception ex)
                {
                    LogHelper.WriteLog(string.Format("执行产量查询时出现错误【{0}】", ex.Message), ex);
                    throw new Exception(ex.Message);
                }
                finally
                {
                    Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        this.IsVisibleLoading = Visibility.Collapsed;//隐藏加载动画
                            this.IsEnabledQueryButton = true;
                    }));
                }


            });

        }
        /// <summary>
        /// 导出数据
        /// </summary>
        private async void ExportAsync()
        {
            if (dtOutput.Rows.Count == 0)
            {
                MessageBox.Show("没有需要导出的数据", "软件温馨提示!", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            if (!IsEnabledQueryButton)
            {
                MessageBox.Show("正在查询数据,待查询完成后再点击导出按钮.", "软件温馨提示!", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            IsEnabledExportButton = false;//将完成导出状态改成false防止用户再导报告时点击查询数据
            //保存数据之前先关闭Excel
            BusinessProcess.KillProcess("EXCEL");
            this.IsEnabledExportButton = false;
            //创建Excel  
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook excelWB = excelApp.Workbooks.Add(System.Type.Missing);    //创建工作簿（WorkBook：即Excel文件主体本身）  
            Microsoft.Office.Interop.Excel.Worksheet excelWS = (Microsoft.Office.Interop.Excel.Worksheet)excelWB.Worksheets[1];   //创建工作表（即Excel里的子表sheet） 1表示在子表sheet1里进行数据导出  
            //excelWS.Cells.NumberFormat = "@";     //  如果数据中存在数字类型 可以让它变文本格式显示  
            //将数据导入到工作表的单元格
            await Task.Run(() => {
                //显示加载动画
                this.IsVisibleLoading = Visibility.Visible;
                for (int i = 0; i < dtOutput.Columns.Count; i++)
                {
                    excelWS.Cells[1, i + 1] = dtOutput.Columns[i].ColumnName;
                }

                for (int i = 0; i < dtOutput.Rows.Count; i++)
                {
                    for (int j = 0; j < dtOutput.Columns.Count; j++)
                    {
                        excelWS.Cells[i + 2, j + 1] = dtOutput.Rows[i][j].ToString();   //Excel单元格第一个从索引1开始 
                    }
                }
                //关闭加载动画
                this.IsVisibleLoading = Visibility.Collapsed;
            });

            //使用对话框让用户选择保存位置和填写保存的文件名
            Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();

            sfd.Filter = "Excel工作簿(*.xlsx)|*.xlsx";

            sfd.FilterIndex = 1;

            sfd.RestoreDirectory = true;

            if (sfd.ShowDialog() == true)
            {
                string localFilePath = sfd.FileName.ToString(); //获得文件路径
                string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1); //获取文件名，不带路径

                //获取文件路径，不带文件名
                //FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));
                //给文件名前加上时间
                //newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt;
                //在文件名里加字符
                //saveFileDialog1.FileName.Insert(1,"dameng");
                //System.IO.FileStream fs = (System.IO.FileStream)sfd.OpenFile();//输出文件fs输出带文字或图片的文件，就看需求了
                excelWB.SaveAs(localFilePath);  //将其进行保存到指定的路径  
                excelWB.Close();
                excelApp.Quit();  //KillAllExcel(excelApp); 释放可能还没释放的进程
                MessageBox.Show("导出表格成功!","软件温馨提示!",MessageBoxButton.OK,MessageBoxImage.Information);
                System.Diagnostics.Process.Start(localFilePath);//保存完成后  打开文件
            }
            this.IsEnabledExportButton = true;
        }

        /// <summary>
        /// 页面加载事件
        /// </summary>
        private void LoadedPage()
        {
            FillWOToCombobox();
            FillPNToCombobox();
            FillStationToCombobox();
            FillWeekDayToCombobox();
            FillWeekToCombobox();
            FillYearToCombobox();
            FillShiftToCombobox();
        }
        /// <summary>
        /// 填充星期信息
        /// </summary>
        private void FillWeekDayToCombobox()
        {
            WeekDays = new ObservableCollection<ProductionPlanAndActualEntity>();
            WeekDays.Add(new ProductionPlanAndActualEntity { WeekDay = "全部", WeekDayValue = "All" });
            WeekDays.Add(new ProductionPlanAndActualEntity { WeekDay = "一", WeekDayValue = "Monday" });
            WeekDays.Add(new ProductionPlanAndActualEntity { WeekDay = "二", WeekDayValue = "Tuesday" });
            WeekDays.Add(new ProductionPlanAndActualEntity { WeekDay = "三", WeekDayValue = "Wednesday" });
            WeekDays.Add(new ProductionPlanAndActualEntity { WeekDay = "四", WeekDayValue = "Thursday" });
            WeekDays.Add(new ProductionPlanAndActualEntity { WeekDay = "五", WeekDayValue = "Firday" });
            WeekDays.Add(new ProductionPlanAndActualEntity { WeekDay = "六", WeekDayValue = "Saturday" });
            WeekDays.Add(new ProductionPlanAndActualEntity { WeekDay = "日", WeekDayValue = "Sunday" });
        }
        /// <summary>
        /// 填充机种到下拉列表框
        /// </summary>
        private void FillPNToCombobox()
        {
            DataTable dtPNs = new DataTable();
            dtPNs = BusinessProcess.GetPNs(EnumPNType.Filter);
            PNs = new ObservableCollection<PartNumberEntity>();
            PNs.Add(new PartNumberEntity { PN ="全部" });
            for (int i = 0; i < dtPNs.Rows.Count; i++)
            {
                PNs.Add(new PartNumberEntity { PN = dtPNs.Rows[i]["PartNumber"].ToString() });
            }
        }
        /// <summary>
        /// 填充工序到下拉列表框
        /// </summary>
        private void FillStationToCombobox()
        {
            List<string> lstStations = new List<string>();
            lstStations = Global.AppConfigs.ProductionPlanInfo.Station.Split('|').ToList<string>();
            Stations = new ObservableCollection<StationsEntity>();
            Stations.Add(new StationsEntity { Station = "全部" });
            for (int i = 0; i < lstStations.Count; i++)
            {
                Stations.Add(new StationsEntity { Station = lstStations[i] });
            }
        }
        //填充工单信息到下拉列表框
        private void FillWOToCombobox()
        {
            DataTable dtWos = new DataTable();
            dtWos = BusinessProcess.GetWOInfo("*");
            WOs = new ObservableCollection<WOEntity>();
            WOs.Add(new WOEntity { WO = "全部" });
            for (int i = 0; i < dtWos.Rows.Count; i++)
            {
                WOs.Add(new WOEntity { WO = dtWos.Rows[i]["WO"].ToString() });
            }
        }
        //填充班别信息到下拉列表框
        private void FillShiftToCombobox()
        {
            List<string> lstShifts = new List<string>();
            lstShifts = Global.AppConfigs.ProductionPlanInfo.Shift.Split('|').ToList<string>();
            Shifts = new ObservableCollection<ProductionPlanEntity>();
            Shifts.Add(new ProductionPlanEntity { Shift = "全部" });
            for (int i = 0; i < lstShifts.Count; i++)
            {
                Shifts.Add(new ProductionPlanEntity { Shift = lstShifts[i] });
            }
        }
        //填充周别信息到下拉列表框
        private void FillWeekToCombobox()
        {
            List<string> lstWeeks = new List<string>();
            lstWeeks = Global.AppConfigs.ProductionPlanInfo.Week.Split('|').ToList<string>();
            Weeks = new ObservableCollection<ProductionPlanEntity>();
            for (int i = 0; i < lstWeeks.Count; i++)
            {
                Weeks.Add(new ProductionPlanEntity { Week = lstWeeks[i] });
            }
        }
        //填充年份信息到下拉列表框
        private void FillYearToCombobox()
        {
            List<string> lstYears = new List<string>();
            lstYears = Global.AppConfigs.ProductionPlanInfo.Year.Split('|').ToList<string>();
            Years = new ObservableCollection<ProductionPlanEntity>();
            for (int i = 0; i < lstYears.Count; i++)
            {
                Years.Add(new ProductionPlanEntity { Year = lstYears[i] });
            }
        }
        /// <summary>
        /// 筛选工单下拉列表框中的数据
        /// </summary>
        private void FilterWO()
        {

            IsOpenComboWODropDown = true;
            //当是第一次过滤的时候备份原始数据
            //这样是为了当不输入搜索条件时添加原始数据到列表中
            //当输入搜索条件时生成过滤集合绑定当下拉列表中
            if (isFirstFilterWO)
            {
                backupWOs = new ObservableCollection<WOEntity>();
                backupWOs = WOs;
                isFirstFilterWO = false;
            }
            if (!string.IsNullOrEmpty(SearchWOText) && WOs != null)
            {
                IEnumerable<WOEntity> WOEntities = WOs.Where(s => s.WO.ToUpper().Contains(SearchWOText.ToUpper()));
                WOs = new ObservableCollection<WOEntity>();
                foreach (var w in WOEntities)
                {
                    WOs.Add(w);
                }
            }
            else
            {
                WOs = backupWOs;
            }

        }
        /// <summary>
        /// 筛选机种下拉列表框中的数据
        /// </summary>
        private void FilterPN()
        {

            IsOpenComboPNDropDown = true;
            //当是第一次过滤的时候备份原始数据
            //这样是为了当不输入搜索条件时添加原始数据到列表中
            //当输入搜索条件时生成过滤集合绑定当下拉列表中
            if (isFirstFilterPN)
            {
                backupPNs = new ObservableCollection<PartNumberEntity>();
                backupPNs = PNs;
                isFirstFilterPN = false;
            }
            if (!string.IsNullOrEmpty(SearchPNText))
            {
                IEnumerable<PartNumberEntity> partNumberEntities = PNs.Where(s => s.PN.ToUpper().Contains(SearchPNText.ToUpper()));
                PNs = new ObservableCollection<PartNumberEntity>();
                foreach (var p in partNumberEntities)
                {
                    PNs.Add(p);
                }
            }
            else
            {
                PNs = backupPNs;
            }

        }
        /// <summary>
        /// 筛选工序下拉列表框中的数据
        /// </summary>
        private void FilterStation()
        {
            IsOpenComboStationDropDown = true;
            //当是第一次过滤的时候备份原始数据
            //这样是为了当不输入搜索条件时添加原始数据到列表中
            //当输入搜索条件时生成过滤集合绑定当下拉列表中
            if (isFirstFilterStation)
            {
                backupStations = new ObservableCollection<StationsEntity>();
                backupStations = Stations;
                isFirstFilterStation = false;
            }
            if (!string.IsNullOrEmpty(SearchStationText) && Stations != null)
            {
                IEnumerable<StationsEntity> StationEntities = Stations.Where(s => s.Station.ToUpper().Contains(SearchStationText.ToUpper()));
                Stations = new ObservableCollection<StationsEntity>();
                foreach (var s in StationEntities)
                {
                    Stations.Add(s);
                }
            }
            else
            {
                Stations = backupStations;
            }

        }
        private void FilterWeekDay()
        {
            IsOpenComboWeekDayDropDown = true;
            //当是第一次过滤的时候备份原始数据
            //这样是为了当不输入搜索条件时添加原始数据到列表中
            //当输入搜索条件时生成过滤集合绑定当下拉列表中
            if (isFirstFilterWeekDay)
            {
                backupWeekDays = new ObservableCollection<ProductionPlanAndActualEntity>();
                backupWeekDays = WeekDays;
                isFirstFilterWeekDay = false;
            }
            if (!string.IsNullOrEmpty(SearchWeekDayText) && WeekDays != null)
            {
                IEnumerable<ProductionPlanAndActualEntity> WeekDayEntities = WeekDays.Where(wd => wd.WeekDay.ToUpper().Contains(SearchWeekDayText.ToUpper()));
                WeekDays = new ObservableCollection<ProductionPlanAndActualEntity>();
                foreach (var w in WeekDayEntities)
                {
                    WeekDays.Add(w);
                }
            }
            else
            {
                WeekDays = backupWeekDays;
            }
        }
        /// <summary>
        /// 筛选周别下拉列表框中的数据
        /// </summary>
        private void FilterWeek()
        {
            IsOpenComboWeekDropDown = true;
            //当是第一次过滤的时候备份原始数据
            //这样是为了当不输入搜索条件时添加原始数据到列表中
            //当输入搜索条件时生成过滤集合绑定当下拉列表中
            if (isFirstFilterWeek)
            {
                backupWeeks = new ObservableCollection<ProductionPlanEntity>();
                backupWeeks = Weeks;
                isFirstFilterWeek = false;
            }
            if (!string.IsNullOrEmpty(SearchWeekText) && Weeks != null)
            {
                IEnumerable<ProductionPlanEntity> WeekEntities = Weeks.Where(w => w.Week.ToUpper().Contains(SearchWeekText.ToUpper()));
                Weeks = new ObservableCollection<ProductionPlanEntity>();
                foreach (var w in WeekEntities)
                {
                    Weeks.Add(w);
                }
            }
            else
            {
                Weeks = backupWeeks;
            }

        }
        /// <summary>
        /// 筛选年份下拉列表框中的数据
        /// </summary>
        private void FilterYear()
        {
            IsOpenComboYearDropDown = true;
            //当是第一次过滤的时候备份原始数据
            //这样是为了当不输入搜索条件时添加原始数据到列表中
            //当输入搜索条件时生成过滤集合绑定当下拉列表中
            if (isFirstFilterYear)
            {
                backupYears = new ObservableCollection<ProductionPlanEntity>();
                backupYears = Years;
                isFirstFilterYear = false;
            }
            if (!string.IsNullOrEmpty(SearchYearText) && Years != null)
            {
                IEnumerable<ProductionPlanEntity> YearEntities = Years.Where(y => y.Year.ToUpper().Contains(SearchYearText.ToUpper()));
                Years = new ObservableCollection<ProductionPlanEntity>();
                foreach (var y in YearEntities)
                {
                    Years.Add(y);
                }
            }
            else
            {
                Years = backupYears;
            }
        }
        #endregion
    }
}
